package de.QuizCommunity;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;
import java.util.Locale;
import java.util.Timer;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.facebook.android.AsyncFacebookRunner;
import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.FacebookError;
import com.facebook.android.Util;
import com.facebook.android.AsyncFacebookRunner.RequestListener;
import com.facebook.android.Facebook.DialogListener;

import de.QuizCommunity.maps.GPSTracker;
import de.QuizCommunity.util.AppVariabels;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Address;
import android.location.Geocoder;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.Interpolator;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class Startseite extends Activity {
	
    private SharedPreferences mPrefs;
    
	@Override
	public void onCreate(Bundle icicle) {
		
	super.onCreate(icicle);
	setContentView(R.layout.startseite);

	Timer tm = new Timer();
	
	ConnectivityManager cm =
	        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo netInfo = cm.getActiveNetworkInfo();
	    if (netInfo==null || netInfo.getState()==NetworkInfo.State.DISCONNECTED || netInfo.getState() == NetworkInfo.State.DISCONNECTING) {
	       
	    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(
					"Es besteht kein Internetzugriff. Bitte aktivieren sie ihr W-Lan oder 3G Netz.")
					.setCancelable(false)
					.setPositiveButton("Ok",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									Intent i = new Intent(
											Settings.ACTION_WIFI_SETTINGS);
									startActivityForResult(i, 1);
									
								}
							}).show();
	        
	        
	        
	    }
	
	final Button button = (Button) findViewById(R.id.button1);
	final ImageView iVlogo = (ImageView) findViewById(R.id.imageView1);
	final ImageView iVwillk1 = (ImageView) findViewById(R.id.imageView2);
	final ImageView iVwillk2 = (ImageView) findViewById(R.id.imageView3);
	final ImageView iVwillk3 = (ImageView) findViewById(R.id.imageView4);
	final ImageView iVwillk4 = (ImageView) findViewById(R.id.imageView5);
	
	final Animation animBounce = AnimationUtils.loadAnimation(this, R.anim.bounce);

	iVwillk1.startAnimation(animBounce);

	iVwillk2.startAnimation(animBounce);

	iVwillk3.startAnimation(animBounce);

	iVwillk4.startAnimation(animBounce);

	TextView tv = (TextView) findViewById(R.id.textView1);
	ImageView iVPfeil1 = (ImageView) findViewById(R.id.imageView8);
	ImageView iVPfeil2 = (ImageView) findViewById(R.id.imageView9);
	
	tv.setBackgroundResource(R.layout.border); 
	
	Animation animborder= AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
	animborder.setDuration(10000);
	animborder.setAnimationListener(new AnimationListener(){

		public void onAnimationEnd(Animation animation) {
			// TODO Auto-generated method stub
			
		}

		public void onAnimationRepeat(Animation animation) {
			// TODO Auto-generated method stub
			
		}

		public void onAnimationStart(Animation animation) {
			// TODO Auto-generated method stub
			
		}
		
	});
	tv.startAnimation(animborder);
	
	Animation anim = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
	anim.setDuration(10000);
	anim.setAnimationListener(new AnimationListener(){

		public void onAnimationEnd(Animation animation) {
			// TODO Auto-generated method stub
			
		}

		public void onAnimationRepeat(Animation animation) {
			// TODO Auto-generated method stub
			
		}

		public void onAnimationStart(Animation animation) {
			// TODO Auto-generated method stub
			
		}
		
	});
	iVlogo.startAnimation(anim);
	iVlogo.setVisibility(1);
	
	
	Animation animPfeil= AnimationUtils.loadAnimation(this, R.anim.tween);
	animPfeil.setAnimationListener(new AnimationListener(){

		public void onAnimationEnd(Animation animation) {
			// TODO Auto-generated method stub
			
		}

		public void onAnimationRepeat(Animation animation) {
			// TODO Auto-generated method stub
			
		}

		public void onAnimationStart(Animation animation) {
			// TODO Auto-generated method stub
			
		}
		
	});
	iVPfeil1.startAnimation(animPfeil);
	iVPfeil2.startAnimation(animPfeil);
	
	GPSTracker gps = new GPSTracker(this);
	if (gps.canGetLocation()) {
		// ermittle Standortname und vermerke diesen
		Geocoder gcd = new Geocoder(this, Locale.getDefault());
		List<Address> addresses;
		try {
			addresses = gcd.getFromLocation(gps.getLatitude(),
					gps.getLongitude(), 1);
			if (addresses.size() > 0) {
				AppVariabels appVar = AppVariabels.getInstance();
				appVar.setAktuellerStandort(addresses.get(0).getLocality());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	} else {
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setMessage(
					"Standort nicht ermittelbar. Schalten Sie Bitte das GPS oder WiFi ein.")
					.setCancelable(false)
					.setPositiveButton("OK",
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int id) {
									Intent i = new Intent(
											Settings.ACTION_LOCATION_SOURCE_SETTINGS);
									startActivityForResult(i, 1);
								}
							}).show();
	}
	
	button.setOnClickListener(new View.OnClickListener() {
		public void onClick(View v) {
			
			Intent in = new Intent(Startseite.this,Login_Facebook.class);
			//Intent in = new Intent(Startseite.this,Menue.class);
			//Intent in = new Intent(Startseite.this, Menue.class);
			startActivity(in);
			
		}
		
	});
	

	}
}
